//
//  ConiditionDetailViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/26.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "ConiditionDetailViewController.h"
#import "ConiditionDetailCollectionCell.h"
#import "DetailConditionModel.h"
#import "ConditionTableViewCell.h"
@interface ConiditionDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    NSInteger sectionCount;
}
@end

@implementation ConiditionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [[NSMutableArray alloc]init];
    self.tableArr = [[NSMutableArray alloc]init];
    self.bigArray = [[NSMutableArray alloc]init];
    

    [self creatCollectionView];
    [self parseData];
}

-(void)creatCollectionView
{
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake((mainScreen_Width-suitableScreen_Width*30)/2, mainScree_Height/3);
    _flowLayout.minimumInteritemSpacing = 10;
    _flowLayout.minimumLineSpacing = 10;
    [_flowLayout setSectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height-64) collectionViewLayout:_flowLayout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerClass:[ConiditionDetailCollectionCell class] forCellWithReuseIdentifier:@"ConiditionDetailCollectionCell"];
    
}
-(void)parseData
{
    [AFNblock getdataWithString:self.webStr block:^(id block) {
            for (NSDictionary *dic in [block objectForKey:@"series"]) {
            ConditionModel *model = [ConditionModel modelWithDictionary:dic];
            [self.dataArray addObject:model];
             self.detailArray = [[NSMutableArray alloc]init];
            for (NSDictionary *diction in [dic objectForKey:@"subSeriesList"]) {
                for (NSDictionary *dicArr in [diction objectForKey:@"cars"]) {
                    
                    DetailConditionModel *detailModel = [DetailConditionModel modelWithDictionary:dicArr];
                    [_detailArray addObject:detailModel];
                }
            }
     
            [self.bigArray addObject:self.detailArray];
            

        }
        if (self.bigArray.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"未搜到相关内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }

        [_collection reloadData];
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ConiditionDetailCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ConiditionDetailCollectionCell" forIndexPath:indexPath];
    ConditionModel *model = [self.dataArray objectAtIndex:indexPath.item];
//    cell.contentView.backgroundColor = [UIColor orangeColor];
    [cell cellData:model];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableArr = [_bigArray objectAtIndex:indexPath.item];
    [self creatTable];
    
}
-(void)xiaoshi
{
    [UIView animateWithDuration:.5 animations:^{
        
        [self.tabeleV removeFromSuperview];
        self.tabeleV = nil;
    }];
}
-(void)creatTable
{
    _tabeleV = [[UITableView alloc]initWithFrame:CGRectMake(mainScreen_Width*.4, 0, mainScreen_Width*.6, mainScree_Height-64) style:UITableViewStylePlain];
    _tabeleV.dataSource = self;
    _tabeleV.delegate = self;
    UISwipeGestureRecognizer *tap = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(xiaoshi)];
    tap.cancelsTouchesInView = NO;
    [self.tabeleV addGestureRecognizer:tap];
    [self.view addSubview:_tabeleV];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return suitableScreen_Height*100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"cell";
    ConditionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[ConditionTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    DetailConditionModel *model = [_tableArr objectAtIndex:indexPath.row];
    [cell cellForModel:model];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
